<template>
  <div>
    <!--工具栏-->
    <div class="head-container crud-opts ui-flex">
      <div class="ui-flex-1">
        <el-input
          size="mini"
          v-model="queryParam.keyword"
          class="ui-w-160"
          placeholder="请输入标题"
          @keyup.enter.native="init()"
        ></el-input>
        <el-select
          size="mini"
          v-model="queryParam.type"
          placeholder="审批类型"
          @change="resetPage()"
          clearable
          filterable
          class="ui-w-120"
        >
          <el-option
            v-for="item in dict.sys_approval_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-select
          size="mini"
          v-model="queryParam.status"
          placeholder="审批状态"
          @change="resetPage()"
          clearable
          filterable
          class="ui-w-120"
        >
          <el-option
            v-for="item in dict.sys_approval_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div class="crud-opts-right ui-flex">
        <el-button
          type="primary"
          size="mini"
          @click="deleteFn()"
          v-permission="permission.del"
          :disabled="multipleSelection.length <= 0"
          >批量删除</el-button
        >
      </div>
    </div>

    <!--表格渲染-->
    <el-table
      :data="tableData"
      height="calc(100vh - 170px)"
      style="width: 100%; margin-bottom: 15px"
      border
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column
        prop="noticeTitle"
        label="公告标题"
        sortable
        min-width="200"
      ></el-table-column>
      <el-table-column
        prop="noticeType"
        label="公告类型"
        sortable
        min-width="80"
      >
        <template slot-scope="scope">
          {{ getPubTypeStr(scope.row.noticeType, dict.sys_approval_type) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" sortable width="100">
        <template slot-scope="scope">
          <disabled-tag
            :status="scope.row.status"
            :options="dict.sys_approval_status"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建者" sortable width="100">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="110">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="editFn(scope.row)"
            v-permission="permission.edit"
            >修改</el-button
          >
          <el-button
            v-permission="permission.del"
            type="text"
            @click="delFn(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageParam.page"
      :page-sizes="pageSizes"
      :page-size="pageParam.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { parseTime } from "@/utils";
import publice from "@/mixins/publice.js";
import { getNoticeAll, postNoticeDel } from "@http/information/notice.js";

export default {
  components: {},
  name: "dns",
  mixins: [publice],
  dicts: ["sys_approval_type", "sys_approval_status"],
  data() {
    return {
      queryParam: {
        keyword: "",
        type: "",
        user: "",
        status: "",
      },
      isEdit: false,
      tableData: [],
      loading: false,
      dialogVisible: false,
      permission: {
        list: ["admin", "notice:list"],
        add: ["admin", "notice:add"],
        edit: ["admin", "notice:edit"],
        del: ["admin", "notice:del"],
      },
      multipleSelection: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    /**
     * @Author: Songjq
     * @Date: 2021-12-08 23:27:27
     * @Fn: 获取所有数据
     */
    init() {
      this.loading = true;
      getNoticeAll(Object.assign(this.queryParam, this.pageParam)).then(
        (res) => {
          res.data.forEach((val) => {
            val.createTime = parseTime(
              val.createTime,
              "{y}-{m}-{d} {h}:{i}:{s}"
            );
          });
          this.tableData = res.data;
          this.total = res.total;
          this.loading = false;
        }
      );
    },

    /**
     * @Author: Songjq
     * @Date: 2021-12-08 23:32:55
     * @Fn:编辑
     */
    editFn(row) {
      this.$refs.add.dialogVisible = true;
      this.isEdit = true;
      this.$nextTick(() => {
        this.$refs.add.init(JSON.parse(JSON.stringify(row)));
      });
    },

    /**
     * @Author: Songjq
     * @Date: 2021-12-25 18:42:39
     * @Fn: 多选操作
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    /**
     * @Author: Songjq
     * @Date: 2021-12-08 23:34:35
     * @Fn: 删除
     */
    delFn(row) {
      this.multipleSelection = [row];
      this.deleteFn();
    },

    /**
     * @Author: Songjq
     * @Date: 2021-12-08 23:34:35
     * @Fn: 删除
     */
    deleteFn(row) {
      if (this.multipleSelection.length <= 0) {
        this.$message.error({
          type: "info",
          message: "至少选中一条数据",
        });
        return;
      }

      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let ids = [];
          this.multipleSelection.forEach((val) => {
            ids.push(val._id);
          });
          postNoticeDel({ ids }).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.init();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss"></style>